<template>
  <div class="profile-container">
    <!-- 顶部导航栏 -->
    <header class="profile-header">
      <div class="header-content">
        <button class="back-btn" @click="goBack">
          <svg width="28" height="28" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M15 18L9 12L15 6" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
        <h1>个人中心</h1>
      </div>
    </header>

    <!-- 用户信息卡片 -->
    <div class="profile-card">
      <div class="user-avatar-large">
        {{ username.charAt(0).toUpperCase() }}
      </div>
      <div class="user-details">
        <h2>{{ username }}</h2>
        <p class="user-id">用户ID: {{ userInfo?.id || 'N/A' }}</p>
        <p class="user-email" v-if="userInfo?.email">{{ userInfo.email }}</p>
      </div>
    </div>

    <!-- 统计信息 -->
    <div class="stats-grid">
      <div class="stat-item">
        <div class="stat-value">{{ userInfo?.workCount || 0 }}</div>
        <div class="stat-label">作品数量</div>
      </div>
      <div class="stat-item">
        <div class="stat-value">{{ userInfo?.likeCount || 0 }}</div>
        <div class="stat-label">获赞数</div>
      </div>
      <div class="stat-item">
        <div class="stat-value">{{ userInfo?.followerCount || 0 }}</div>
        <div class="stat-label">粉丝数</div>
      </div>
    </div>

    <!-- 功能菜单 -->
    <div class="menu-section">
      <div class="menu-item" @click="goToMyWorks">
        <div class="menu-icon">🎨</div>
        <div class="menu-text">我的作品</div>
        <div class="menu-arrow">›</div>
      </div>
      <div class="menu-item" @click="goToSettings">
        <div class="menu-icon">⚙️</div>
        <div class="menu-text">账户设置</div>
        <div class="menu-arrow">›</div>
      </div>
      <div class="menu-item" @click="handleLogout">
        <div class="menu-icon">🚪</div>
        <div class="menu-text">退出登录</div>
        <div class="menu-arrow">›</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { logout } from '../api/auth'

// 定义用户信息类型
interface UserInfo {
  username: string;
  email?: string;
  id?: string;
  workCount?: number;
  likeCount?: number;
  followerCount?: number;
  [key: string]: any;
}

const router = useRouter()
const userInfo = ref<UserInfo | null>(null)
const username = ref('')

// 获取用户名
const getUsername = () => {
  return userInfo.value?.username || '用户'
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 跳转到我的作品页面
const goToMyWorks = () => {
  // 这里可以跳转到用户的作品页面
  console.log('跳转到我的作品页面')
}

// 跳转到设置页面
const goToSettings = () => {
  // 这里可以跳转到账户设置页面
  console.log('跳转到账户设置页面')
}

// 处理登出
const handleLogout = async () => {
  try {
    // 立即清除本地用户信息并跳转，提升响应速度
    localStorage.removeItem('userInfo')
    userInfo.value = null
    
    // 立即跳转到登录页
    router.push('/')
    
    // 在后台执行登出请求（不影响用户界面）
    try {
      await logout()
      console.log('用户已登出')
    } catch (error: any) {
      console.error('登出请求失败:', error)
      // 即使后端登出失败，前端状态已经清理，不影响用户体验
    }
  } catch (error: any) {
    console.error('退出失败:', error)
    // 即使失败也清空本地信息并跳转到登录页
    localStorage.removeItem('userInfo')
    userInfo.value = null
    router.push('/')
  }
}

// 加载用户信息
onMounted(() => {
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    try {
      userInfo.value = JSON.parse(storedUserInfo)
      username.value = userInfo.value?.username || '用户'
    } catch (e) {
      console.error('解析用户信息失败:', e)
    }
  }
})
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background: #ffffff;
  padding: 0;
  margin: 0;
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 0;
  left: 0;
  overflow-y: auto;
  box-sizing: border-box;
}

.profile-header {
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 16px 20px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  display: flex;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  background: #667eea;
  border: none;
  cursor: pointer;
  padding: 12px;
  margin-right: 16px;
  border-radius: 50%;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-btn:hover {
  background-color: #5568d3;
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.header-content h1 {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.profile-card {
  background: white;
  border-radius: 16px;
  padding: 30px 20px;
  margin: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-avatar-large {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 32px;
  flex-shrink: 0;
}

.user-details h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.user-id {
  font-size: 14px;
  color: #666;
  margin: 4px 0;
}

.user-email {
  font-size: 14px;
  color: #666;
  margin: 4px 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  padding: 0 20px;
  margin-bottom: 30px;
}

.stat-item {
  background: white;
  border-radius: 12px;
  padding: 20px 16px;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #667eea;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.menu-section {
  background: white;
  border-radius: 16px;
  margin: 0 20px 30px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
  cursor: pointer;
  transition: background-color 0.3s;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item:hover {
  background-color: #f9f9f9;
}

.menu-icon {
  font-size: 20px;
  margin-right: 16px;
  width: 24px;
  text-align: center;
}

.menu-text {
  flex: 1;
  font-size: 16px;
  color: #333;
}

.menu-arrow {
  font-size: 20px;
  color: #999;
}

@media (max-width: 768px) {
  .profile-card {
    margin: 16px;
    padding: 24px 16px;
  }
  
  .stats-grid {
    padding: 0 16px;
    gap: 12px;
  }
  
  .stat-item {
    padding: 16px 12px;
  }
  
  .stat-value {
    font-size: 20px;
  }
  
  .menu-section {
    margin: 0 16px 20px;
  }
  
  .menu-item {
    padding: 14px 16px;
  }
}
</style>